package android.team.app.network;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.team.app.R;
import android.team.app.info.WebserviceConfig;
import android.team.app.ui.BaseActivity;

/**
 * 
 * @author Apple
 * 
 */
public class AsyncHttpResponseProcess implements AsyncHttpResponseListener {

	private BaseActivity baseActivity;// de goi duoc cac phuong thuc ma viet tai
										// lop nay
	private boolean isShowDialog = true;

	public AsyncHttpResponseProcess(BaseActivity activity) {
		this.baseActivity = activity;
	}

	public AsyncHttpResponseProcess(BaseActivity activity, boolean isShow) {
		this.baseActivity = activity;
		this.isShowDialog = isShow;
	}

	@Override
	public void before() {
		// truoc khi load du lieu se xu ly tai day
		// neu load data hien thi dialog
		// neu dong bo khong hien thi
		if (isShowDialog) {
			// tai day se showDialog
			baseActivity.showProgressDialog(baseActivity.getString(R.string.waiting));
		}
	}

	@Override
	public void after(int statusCode, HttpResponse response) {
		// phuong thuc duoc goi khi qua trinh load data ket thuc
		if (isShowDialog) {
			// tai day se dong Dialog
		}

		// tai day se xu ly ket qua tra ve dua vao statusCode(thanh cong thi xu
		// ly, khong thanh cong thi phai thong bao cho nguoi dung loi gi)
		switch (statusCode) {
		case AsyncHttpBase.STATUS_NETWORK_OFF:
			baseActivity.showPositiveCustomDialog(
					baseActivity, 
					R.string.error, 
					R.string.network_disconnect, 
					R.drawable.button_ok);
			// show dialog thong bao network khong co

			break;
		case AsyncHttpBase.STATUS_NETWORK_ERROR:
			baseActivity.showPositiveCustomDialog(
					baseActivity, 
					R.string.error, 
					R.string.network_error, 
					R.drawable.button_ok);
			// show dialog thong bao loi mang

			break;

		case AsyncHttpBase.STATUS_NETWORK_EXCEIPTION:
			baseActivity.showPositiveCustomDialog(
					baseActivity, 
					R.string.error, 
					R.string.network_error, 
					R.drawable.button_ok);
			// show dialog thong bao exceiption

			break;

		case AsyncHttpBase.STATUS_NETWORK_TIMEOUT:
			baseActivity.showPositiveCustomDialog(
					baseActivity, 
					R.string.error, 
					R.string.network_timeout, 
					R.drawable.button_ok);
			// show dialog thong bao xu ly lau qua (thoi khog can)

			break;

		case AsyncHttpBase.STATUS_NETWORK_OK:

			// day la trang thai nhan response thanh cong
			// xu ly xem response nhu the nao

			break;

		default:
			break;
		}
	}
	
	public void processHttpResponse(HttpResponse response){
		//tai day dua vao response se lay du lieu kieu string
		String jsonString = "";
		
		try {
			jsonString = EntityUtils.toString(response.getEntity());
			
			if(jsonString == null){
				//thong bao loi du lieu
				baseActivity.showPositiveCustomDialog(
						baseActivity, 
						R.string.error, 
						R.string.json_parser_error, 
						R.drawable.button_ok);
				return;
			}else{
				//bat dau xu ly
				int success = 0;//that bai// 1 thanh cong
				JSONObject jsonObject = new JSONObject(jsonString);
				success = jsonObject.getInt(WebserviceConfig.PARAM_SUCCESS);
				
				if(success == 1){
					//thanh cong
					processIfResponseSuccess(jsonObject);
				}else{
					//that bai
					processIfResponseFail();
				}
				
				
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	//xay dung phuong thuc de xu ly du lieu thanh cong
	public void processIfResponseSuccess(JSONObject json){
		
	}
	
	//xay dung phuong thuc de xu ly du lieu khi that bai
	public void processIfResponseFail(){
		
	}
}
